package com.qf.fmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.fmall.product.entity.ProductComments;
import com.qf.fmall.product.mapper.ProductCommentsMapper;
import com.qf.fmall.product.service.IProductCommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall.product.vo.CommentCountResponseVo;
import com.qf.fmall.product.vo.ProductCommentResponseVo;
import com.qf.fmall.user.entity.User;
import com.qf.fmall.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author djl
 * @since 2024-04-26
 */
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements IProductCommentsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ProductCommentResponseVo detailCommonts(Integer id, Integer pageNum, Integer limit) {

        //1. 查询商品评论表获取商品的评论信息
        // select * from product_comments where product_id=id limit ?,?

        Page<ProductComments> page = new Page<>(pageNum, limit);
        this.page(page,new QueryWrapper<ProductComments>().eq("product_id",id));

        long total = page.getTotal();
        long pages = page.getPages();
        List<ProductComments> records = page.getRecords();

        //2. 查user表。获取评论用户对应的信息
        for (ProductComments record : records) {
            // 查询user表获取用户信息
            // select * from user where user_id = record.userid

            User user = userMapper.selectById(record.getUserId());
            record.setUsername(user.getUsername());
            record.setNickname(user.getNickname());
            record.setUserImg(user.getUserImg());
        }

        return new ProductCommentResponseVo(pages, total, records);

    }

    @Override
    public CommentCountResponseVo detailcommontscount(Integer id) {

        //1. 查询好评条数
        // select count(*) from product_comments where product_id=id and comm_type=1
        long goodTotal = this.count(new QueryWrapper<ProductComments>().eq("product_id", id).eq("comm_type", 1));

        //2. 查询中评条数
        long midTotal = this.count(new QueryWrapper<ProductComments>().eq("product_id", id).eq("comm_type", 0));

        //3. 查询差评条数
        long badTotal = this.count(new QueryWrapper<ProductComments>().eq("product_id", id).eq("comm_type", -1));

        //4. 总条件
        long total = goodTotal + midTotal + badTotal;

        //5. 好评率计算
        BigDecimal divide = new BigDecimal(goodTotal + "").multiply(new BigDecimal("100")).divide(new BigDecimal(total + ""), 2, RoundingMode.HALF_UP);

        String percent = divide.toString();

        return new CommentCountResponseVo(goodTotal,total,badTotal,midTotal,percent);
    }
}
